<template>
  <div class="login-page">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span class="login-title">登录</span>
      </div>
      <div class="login-form">
        <el-form :model="form" :rules="loginRules" ref="loginForm">
          <el-form-item prop="userName">
            <el-input type="text" v-model="form.userName" auto-complete="off" placeholder="请输入用户名">
              <template slot="prepend"><i style="font-size:20px" class="el-icon-user"></i></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="passWord">
            <el-input type="text" v-model="form.passWord" auto-complete="off" placeholder="请输入密码" show-password>
              <template slot="prepend"><i style="font-size:20px" class="el-icon-key"></i></template>
            </el-input>
          </el-form-item>

          <div style="display: flex; justify-content: space-between;">
            <el-form-item style="flex-grow: 1; margin-right: 10px;">
              <el-button style="width:100%;" type="primary" @click="handleLogin" :loading="loading">登录</el-button>
            </el-form-item>
            <el-form-item style="flex-grow: 1;">
              <el-button style="width:100%;" type="primary" @click="goToRegister" :loading="loading">注册</el-button>
            </el-form-item>
          </div>


        </el-form>
      </div>
    </el-card>
  </div>
</template>


<script>
export default {
  name: 'LoginComponent',
  data() {
    return {
      loading: false,
      form: {
        userName: '',
        passWord: ''
      },
      loginRules: {
        userName: [
          { required: true, message: '请输入账户', trigger: 'blur' },
        ],
        passWord: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    goToRegister() {
      this.$router.push('/register')
    },
    handleLogin() {
      this.$axios.get('https://localhost:44300/api/User/Login', {
        params: {
          Username: this.form.userName,
          password: this.form.passWord
        }
      }).then(res => {
        if (res.data.success == true) {
          this.$message({
            message: '登录成功',
            type: 'success'
          });
          if (res.data.response.role === 1) {
            this.$router.push({ name: 'adminMain', params: { userId: res.data.response.userId, userName: res.data.response.userName } })

          } else {
            this.$router.push({ name: 'afterLogin', params: { userId: res.data.response.userId, userName: res.data.response.userName } })
          }
        } else {
          this.$message({
            message: '账号或密码错误',
            type: 'error'
          });
        }
      })
    }
  }
}

</script>

<style scoped>
.login-page {
  background: linear-gradient(to bottom, #66a9c9, #ADD8E6);
  height: 97vh;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 0 4px 8px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
}

.login-title {
  font-size: 20px;
}

.box-card {
  width: 375px;
}
</style>

